package com.hlg.xboot.config;

import com.hlg.xboot.web.interceptor.APISignVerifyInterceptor;
import com.hlg.xboot.web.interceptor.APITokenInterceptor;
import com.hlg.xboot.web.interceptor.LogInterceptor;
import com.hlg.xboot.constants.AppConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * <p>拦截器配置</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 中视数讯</p>
 * <p>Site: www.digi-zones.com</p>
 *
 * @author yangxin
 * @version 1.0
 * @date 2016/8/3
 */
@Configuration
@EnableSpringDataWebSupport
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private APITokenInterceptor apiTokenInterceptor;
    @Autowired
    private APISignVerifyInterceptor apiSignVerifyInterceptor;
    @Autowired
    private LogInterceptor logInterceptor;

    private static final String INTERCEPTOR_PATH = "/**";

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        // 参数签名校验
        //registry.addInterceptor(apiSignVerifyInterceptor).addPathPatterns(INTERCEPTOR_PATH);
        // 前端接口会话拦截器
        //registry.addInterceptor(apiTokenInterceptor).addPathPatterns(INTERCEPTOR_PATH);
        // 日志拦截
        //registry.addInterceptor(logInterceptor).addPathPatterns(INTERCEPTOR_PATH);
    }

    @Bean
    public FilterRegistrationBean corsFilterRegistrationBean() {
        // 对响应头进行CORS授权
        MyCorsRegistration corsRegistration = new MyCorsRegistration(INTERCEPTOR_PATH);
        this._configCorsParams(corsRegistration);

        // 注册CORS过滤器
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration(INTERCEPTOR_PATH, corsRegistration.getCorsConfiguration());
        CorsFilter corsFilter = new CorsFilter(configurationSource);
        return new FilterRegistrationBean(corsFilter);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        this._configCorsParams(registry.addMapping(INTERCEPTOR_PATH));
    }

    private void _configCorsParams(CorsRegistration corsRegistration) {
        corsRegistration.allowedOrigins(CorsConfiguration.ALL)
                .allowedMethods(CorsConfiguration.ALL)
                .allowedHeaders(CorsConfiguration.ALL)
                .exposedHeaders(AppConstants.API_TOKEN)
                .allowCredentials(false)
                .maxAge(CrossOrigin.DEFAULT_MAX_AGE * 2);
    }

    public class MyCorsRegistration extends CorsRegistration {

        public MyCorsRegistration(String pathPattern) {
            super(pathPattern);
        }

        @Override
        public CorsConfiguration getCorsConfiguration() {
            return super.getCorsConfiguration();
        }
    }
}
